/**
 * @Title: 心跳类
 * @Package tt.dz.mvc.controller
 * @author pengyu
 * @date 2015年12月17日 上午10:21:55
 * @version V1.0
 */
package tt.dz.mvc.controller.middleware;

import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tt.dz.entity.base.JsonCommon;
import tt.dz.service.middleware.MessageService;
import tt.dz.tcp.socket.exception.RetryFailException;
import tt.dz.util.RedisClient;
import tt.dz.util.StringUtil;
@Controller
public class UpdateTimeController {
	/**
	 * 日志对象
	 */
	private Logger logger = LoggerFactory.getLogger(UpdateTimeController.class);
	@Resource(name = "ev3cMessageServiceImpl")
	MessageService ev3cMessageService;
	
	
	// 3.1.10. 对时指令处理,下发
	@RequestMapping("/updateChargerTime") 
	@ResponseBody
	public JsonCommon<String> updateChargerTime(HttpServletRequest request, HttpServletResponse response) throws Exception {
		String ev3c_msgHeader, ev3c_msgBody;
		JsonCommon<String> json = new JsonCommon<String>();
		Map<String,String> map = StringUtil.getParameterByOms(request);
		String chargerId = request.getParameter("chargerId");
//		String protocol_type = map.get("protocol_type");
//		String protocol_version = map.get("protocol_version");
		if(StringUtil.isBlank(chargerId)){
			json.setCode("400");
			json.setMsg("chargerId null");
			return json;
		}
		ev3c_msgBody = StringUtil.now14time();
		ev3c_msgHeader = "68" + StringUtil.ev3cMsgBodyBin(ev3c_msgBody) + "01" + "000000" + "0001" + chargerId + "34" + StringUtil.now14time();
		
		json.setCode("200");
		json.setMsg("updateTime chargerId = "+chargerId+" success");
		json.setPojo(ev3c_msgHeader+ev3c_msgBody);
		try {
			ev3cMessageService.sendNoWait(ev3c_msgHeader+ev3c_msgBody, chargerId);
		} catch (RetryFailException e) {
			e.printStackTrace();
			json.setCode("400");
			json.setMsg("chargerId "+chargerId+" findHardwareServerIp sendSocket fail "+e.getMessage());
			return json;
		}
		return json;
	}
	
	// case "35": 桩返回对时指令   对时指令处理回调接口
	@RequestMapping("/updateChargerTimeReturn")  //proofreadTimeReturn
	@ResponseBody
	public JsonCommon<String> updateChargerTimeReturn(HttpServletRequest request, HttpServletResponse response) throws Exception {
		JsonCommon<String> json = new JsonCommon<String>();
		Map<String,String> map = StringUtil.getParameterByMiddle(request);
		String chargerId = map.get("chargerId");
		String data = map.get("data");
		String time_temp = data.substring(50);  //
		Date timeFromCharger =  StringUtil.StrToDate(time_temp);
		Date now = new Date();
		if(((now.getTime() - timeFromCharger.getTime())/(1000*60) > 10)||((timeFromCharger.getTime() - now.getTime())/(1000*60) > 10)){
			json.setCode("400");
			json.setMsg("chargerId : "+chargerId+" timeFromCharger not match server!");
			json.setPojo(data);
			//记录到redis中
			String key = chargerId + "_"+ StringUtil.now12time() + "_updateChargerTime";
			map.put("timeFromCharger", time_temp);
			map.put("timeFromServer",StringUtil.now14time());
			RedisClient.putMap(key, map);
			return json;
		}
		json.setCode("200");
		json.setMsg("timeFromCharger match server");
		json.setPojo(data);
		return json;
	}
	
}
